package org.gpp.proj1.demo.part;

import java.io.FileNotFoundException;
import java.io.IOException;

import org.fenggui.Button;
import org.fenggui.ComboBox;
import org.fenggui.composites.Window;
import org.fenggui.event.ButtonPressedEvent;
import org.fenggui.event.IButtonPressedListener;
import org.fenggui.io.IOStreamException;
import org.fenggui.layout.StaticLayout;
import org.fenggui.xml.theme.XMLTheme;
import org.gpp.proj1.bone.parser.BoneStructureParser;
import org.gpp.proj1.bone.parser.BoneStructureParserException;
import org.gpp.proj1.menu.fenggui.FengJMEInputHandler;
import org.gpp.proj1.robot.Robot;
import org.gpp.proj1.robot.RobotImpl;
import org.gpp.proj1.robot.part.SparePart;
import org.gpp.proj1.robot.part.parser.InvalidPartFileException;
import org.gpp.proj1.robot.part.parser.PartFileParser;

import com.jme.app.SimpleGame;
import com.jme.input.FirstPersonHandler;
import com.jme.input.MouseInput;
import com.jme.math.Vector3f;

/**
 * Demo for the part, action, particle and animation systems
 * 
 * @author Paolo Victor, paolovictor@gmail.com
 */
public class PartTest extends SimpleGame implements IButtonPressedListener {
	
	private FengJMEInputHandler fengInput;
	
	private ComboBox list;
	
	private org.fenggui.Display disp;
	
	private Robot robot;
	
	public static void main(String[] args) {		
		PartTest game = new PartTest();
		game.start();
	}
	
    protected void simpleUpdate() {
        fengInput.update( tpf );
        
        if( robot != null ) {
        	robot.updateAnimation( tpf );
        }
    }
		
    @Override
    protected void simpleRender(){
    	disp.display();
    	disp.setDepthTestEnabled( true );
    }
    
		
	@Override
	protected void simpleInitGame() {
		
		FirstPersonHandler handler =  (FirstPersonHandler) this.input;
		handler.getMouseLookHandler().setEnabled( false );
		
		MouseInput.get().setCursorVisible( true );
		
		SparePart part = null, part2 = null, torso = null, legs = null, head = null;
		try {
			part = PartFileParser.parseFile( "res/part/xmlPart/awesomeArm.part" );
			part2 = PartFileParser.parseFile( "res/part/xmlPart/awesomeArm.part" );
			torso = PartFileParser.parseFile( "res/part/xmlPart/awesomeTorso.part" );
			legs = PartFileParser.parseFile( "res/part/xmlPart/awesomeLegs.part" );
			head = PartFileParser.parseFile( "res/part/xmlPart/awesomeHead.part" );
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} catch (InvalidPartFileException e1) {
			e1.printStackTrace();
		}
		
		BoneStructureParser parser = new BoneStructureParser( "res/bone/humanoid.bon" );
		
		try {
			robot = new RobotImpl( parser.getBoneStructure() );
			robot.setPart( "armRight", part );
			robot.setPart( "armLeft", part2 );
			robot.setPart( "torso", torso );
			robot.setPart( "legs", legs );
			robot.setPart( "head", head );
			robot.setAnimation( "stopped" );
		} catch (BoneStructureParserException e) {
			e.printStackTrace();
			System.exit( 1 );
		}
		rootNode.attachChild( robot.getNode() );

		this.cam.setLocation( new Vector3f( 50, 50, 50 ) );
		this.cam.lookAt( new Vector3f( 0, 0, 0 ), new Vector3f( 0, 1, 0 ) );
		
		this.initGUI();
	}
	
	private void initGUI() {
		disp = new org.fenggui.Display(new org.fenggui.render.lwjgl.LWJGLBinding());
		
		try {
			org.fenggui.FengGUI.setTheme(new XMLTheme("data/themes/QtCurve/QtCurve.xml"));
		} catch (IOException e) {
		} catch (IOStreamException e) {
		}
		
		fengInput = new FengJMEInputHandler(disp);
		
		Window frame = new Window(false, false, false);
		disp.addWidget(frame);
		frame.setTitle("Particles");
		frame.setX(50);
		frame.setY(100);
		frame.setSize(220, 100);
		frame.setShrinkable(false);
		frame.setExpandable( false );
		frame.getAppearance().setBorder( null );
		frame.updateMinSize();		
		
		
		frame.getContentContainer().setLayoutManager(new StaticLayout());
		
		list = new ComboBox();
		frame.getContentContainer().addWidget(list);
		list.setShrinkable(false);
		list.setX( 5 );
		list.setY( 50 );
		list.addItem("Flamethrower on right arm");
		list.addItem("Jet on right arm");
		list.setSize(200, list.getMinHeight());
		
		Button button = new Button();
		button.setText( "Execute action");
		button.setSize( button.getMinWidth() + 5, button.getMinHeight() + 5 );
		button.setXY( 5, 10 );
		button.addButtonPressedListener( this );
		
		frame.getContentContainer().addWidget( button );
		
		disp.layout();
	}
	
	public void buttonPressed(ButtonPressedEvent event) {
		if( event.getTrigger().getText().equals( "Execute action" ) ) {
			String action = list.getLabel().getText();
			
			if( action.equals("Flamethrower on right arm") ) {
				robot.executeAction( "armRight", "Flames" );
			} else if( action.equals("Jet on right arm") ) {
				robot.executeAction( "armRight", "Jet" );
			}
		}
	}
}
